// background.js - 视频录制助手后台服务
// 处理基本的消息传递和下载

// 处理消息
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  console.log("background收到消息:", request);
  
  if (request.action === "downloadVideo") {
    try {
      chrome.downloads.download({
        url: request.dataUrl,
        filename: request.filename,
        saveAs: true
      }, function(downloadId) {
        if (chrome.runtime.lastError) {
          console.error("Background下载失败:", chrome.runtime.lastError);
          sendResponse({success: false, error: chrome.runtime.lastError.message});
        } else {
          console.log("Background下载成功，下载ID:", downloadId);
          sendResponse({success: true, downloadId: downloadId});
        }
      });
    } catch (error) {
      console.error("Background下载异常:", error);
      sendResponse({success: false, error: error.message});
    }
    
    return true; // 保持消息通道开放
  }
  
  return true;
});
  